package com.gexin.controller.system.order;

import com.gexin.common.WebConst;
import com.gexin.core.web.SystemBaseController;
import com.gexin.model.order.Order;
import com.gexin.model.order.OrderItem;
import com.gexin.model.order.OrderState;
import com.gexin.model.product.Products;
import com.gexin.model.shop.ShopInfo;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *  订单 
 * @author 方俊逸
 *  2016-06-22
 *
 */

@ControllerBind(controllerKey = "/system/order/push", viewPath = "WEB-INF/system/order")
public class OrderPushController extends SystemBaseController {
	public static List<OrderState> push_state_list =  new ArrayList<>();
	static{
		OrderState t =  new OrderState();
		t.setStateId(1);
		t.setStateName("等待推送");
/*		push_state_list.add(t);
		t =  new OrderState();
		t.setStateId(2);
		t.setStateName("已经推送");
		push_state_list.add(t);
		t.setStateId(3);
		t.setStateName("等待发货");
		t =  new OrderState();*/
		push_state_list.add(t);
	}
	
	public void reSetOrderPush(){
		Order order =  Order.dao.findByIdLoadColumns(getPara("order_id"),"id");
		order.setInterfaceState(1);
		order.setInterfaceTime(new Date());
		order.update();
		success();
	}
	
	public void input_order(){
		Order order =  Order.dao.findByIdLoadColumns(getPara("order_id"),"id");
		order.setInterfaceOderId(getPara("pdd_order_id").trim());
		order.setInterfacePhone(getPara("pdd_phone"));
		order.setInterfaceState(3);
		order.setInterfaceTime(new Date());
		order.update();
		success();
	}
	
	public void  index(){
		 //首先取到所以的订单状态
	    Order order =getModel(Order.class, "order");
	    order.put("product_id", getParaToInt("product_id",null));
	    setAttr("order", order);
	    //如果有状态  ,就返给前端  ,设置activity
	    List<OrderState> state_list=push_state_list;
	    setAttr("state_list", state_list);
	    //根据shop_id  取到所有的大订单 和小订单  
	    Page<Order> page = Order.dao.paginate_push_list(ShopInfo.shop_id, order, getPara("start_time",null),getPara("end_time",null),getpage(), WebConst.Page_Size);
	    paginate(page);
	    List<Order> order_list=page.getList();
	    for(int i=0;i<order_list.size();i++){
	    	List<OrderItem> item_list= OrderItem.dao.getOrderItemsByOrderId(order_list.get(i).getId());
	    	order_list.get(i).put("item", item_list);
	    }
	    setAttr("start_time",getPara("start_time",null));
	    setAttr("end_time",getPara("end_time",null));
	    setAttr("order_list", order_list);
	    
		setAttr("product_list", Products.dao.getAllProductsByShop_id(ShopInfo.shop_id));//查询所有的商品
		render("order_push_list.html");
	}

	
	
	
}


